/**
 * 您的公司.com
 * Copyright (c) 2018-2018 All Rights Reserved.
 */
package com.mei.demo.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 反射调用方法
 *
 * @author mei.xiaoliang@qq.com
 */
public class DemoMethod {

    public static void main(String[] args) {
        try {
            // 对象实例化
            Class<?> dogClass = Class.forName("com.mei.demo.reflect.RefelctDog");
            RefelctDog dog = (RefelctDog) dogClass.newInstance();

            // 调用 set 方法
            Method method1 = dogClass.getDeclaredMethod("setName", String.class);
            method1.invoke(dog, "哈巴狗");

            // 调用 get 方法
            Method method2 = dogClass.getDeclaredMethod("getName");
            String dogName = (String) method2.invoke(dog);
            System.out.println("dogName: " + dogName);

            Method[] methods = dogClass.getMethods();
            for (Method met : methods) {
                System.out.println(met.getName());
            }
            System.out.println("--------------------------------");

            Method[] methods1 = dogClass.getDeclaredMethods();
            for (Method met : methods1) {
                System.out.println(met.getName());
            }

        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

}